import requests
from lxml import etree
from modules.utils.log_util import LOGGER

"""
抓取360热搜
"""

baseurl = "https://tophub.today/n/KqndgxeLl9"
header = {

    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}


def get_weibo_data():
    data = []
    LOGGER.info("请求微博热搜数据")
    try:
        response = requests.get(baseurl, timeout=10, headers=header,verify=False)
        html = response.text
    except Exception as e:
        LOGGER.error(e)
    else:
        try:

            tree = etree.HTML(html)
            tb_node = tree.xpath("//table[@class='table']//tbody")[0]
            tbody = tb_node.xpath(".//tr")
            for node in tbody:
                title = node.xpath(".//td[@class='al']/a/text()")[0]
                url = "https://s.weibo.com/weibo?q=" + title
                hot_index = node.xpath(".//td/text()")[-1]
                item = {"title": title, "hot_index": hot_index, "url": url}
                data.append(item)
        except Exception as e:
            LOGGER.error(e)
    return data


if __name__ == "__main__":
    print(get_weibo_data())
